/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import path = require('path');
import {
  appRoot,
  initByProject,
  initExecuteCommand,
  runHvigorReturnResult,
  runHvigor
} from '../../base/src/common/common';
import { addModule } from '../src/common/common';
import * as fs from 'fs';

describe('test project error status', () => {

  test.concurrent('test hvigorfile.ts/js should exists', async () => {
    await initByProject('error-project-test', async (rootProject) => {
      addModule(rootProject, 'module1');
      const commands = initExecuteCommand({
        tasks: 'assembleHap'
      });
      const hvigorFilePath = path.resolve(rootProject, 'module1', 'hvigorfile.js');
      fs.rmSync(hvigorFilePath);
      const result =await runHvigorReturnResult(rootProject, commands);
      expect(result).toContain(
        `Hvigorfile not found: ${hvigorFilePath.substring(0, hvigorFilePath.lastIndexOf('.'))}.ts/js`);

    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test task status error', async () => {
    await initByProject('error-project-test', async (rootProject) => {
      addModule(rootProject, 'module1');
      const commands = initExecuteCommand({
        tasks: 'assembleHap'
      });
      const result =await runHvigorReturnResult(rootProject, commands);
      expect(result).toContain('\'clean\' in module1 lacks the dependency task \'unknown task\'');

    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test task depends circle', async () => {
    await initByProject('error-project-test', async (rootProject) => {
      addModule(rootProject, 'module2');

      const command = initExecuteCommand({
        tasks: 'assembleHap'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain(' Circular dependency between the following tasks: module2:clean3 -> module2:clean1.');
    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test node status error', async () => {
    await initByProject('error-project-test', async (rootProject) => {
      addModule(rootProject, 'module3');
      const command = initExecuteCommand({
        tasks: 'assembleHap'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain(
        'Failed to find the \'unknown node\' node while setting the task dependencies');
    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test node status error', async () => {
    await initByProject('error-project-test', async (rootProject) => {
      addModule(rootProject, 'module4');
      const command = initExecuteCommand({
        tasks: 'assembleHap'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain(
        'Duplicate task: \'clean\'. Rename the task.');
    }, undefined, appRoot(__dirname));
  });
});

describe('test command error status', ()=>{
  test.concurrent('test -m project task should check', async () => {
    await initByProject('common-test', async (rootProject) => {
      const rootProjectName = path.basename(rootProject);
      addModule(rootProject, 'module1');

      const command = initExecuteCommand({
        mode: 'project',
        tasks: 'xxxx'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain(`Task xxxx not found in hvigor node: ${rootProjectName}`);
    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test -m module -p module=module1 task should check', async () => {
    await initByProject('common-test', async (rootProject) => {
      addModule(rootProject, 'module1');

      const command = initExecuteCommand({
        mode: 'module',
        module:'module1',
        tasks: 'xxxx'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain('Task xxxx not found in hvigor node: module1');
    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test -p module=module1 task should check', async () => {
    await initByProject('common-test', async (rootProject) => {
      addModule(rootProject, 'module1');

      const command = initExecuteCommand({
        module:'module1',
        tasks: 'xxxx'
      });
      const result = await runHvigorReturnResult(rootProject, command);
      expect(result).toContain('Task xxxx not found in hvigor node: module1');
    }, undefined, appRoot(__dirname));
  });

  test.concurrent('test -m undefined task should not check', async () => {
    await initByProject('common-test', async (rootProject) => {
      addModule(rootProject, 'module1');

      const command = initExecuteCommand({
        tasks: 'xxxx'
      });
      const result = await runHvigor(rootProject, command);
      expect(result.stderr).toContain('The task to be executed in the project cannot be found.');
      expect(result.stdout).toContain('BUILD SUCCESSFUL');
    }, undefined, appRoot(__dirname));
  });
});
